import { message } from 'antd';
import { history } from 'umi';
import { addUser,fetchShopList,updateUser,fetchRoleList,fetchEmployee,gdWageList,saleWageList,privateWageList} from './service';

const Model = {
  namespace: 'cubeEmployee',
  state: {
    currentEmployee:{},
    shopList:[],
    roleList:[],
    gdWageList:[],
    saleWageList:[],
    privateWageList:[]
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addUser, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/employee/list`)
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateUser, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/employee/list`)
      }else{
        message.error(result.message);
      }
    },
    *fetchShopList({ payload }, { call,put }) {
      let result=yield call(fetchShopList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            shopList:result.data         
           }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchRoleList({ payload }, { call,put }) {
      let result=yield call(fetchRoleList, payload);
      if(result.code===0){
        if(result.data.list){
          yield put({
            type: 'common',
            payload: {
              roleList:result.data.list         
             }
          });
        }
      }else{
        message.error(result.message);
      }
    },
    *fetchEmployee({ payload }, { call,put }) {
      let result=yield call(fetchEmployee, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            currentEmployee:result.data.user||{}
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *gdWageList({ payload }, { call,put }) {
      let result=yield call(gdWageList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            gdWageList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *saleWageList({ payload }, { call ,put}) {
      let result=yield call(saleWageList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            saleWageList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *privateWageList({ payload }, { call,put }) {
      let result=yield call(privateWageList, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            privateWageList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
